#!/bin/bash

:<<eof


练习

变量string="Bigdata process framework is Hadoop,Hadoop is an open source project" 
执行脚本后，打印输出string字符串变量，并给出用户以下选项:

(1)、打印string长度
(2)、删除字符串中所有的Hadoop
(3)、替换第一个Hadoop为Mapreduce
(4)、替换全部Hadoop为Mapreduce

用户输入数字1，2，3，4，可以执行对应项的功能
输入q、Q则退出交互模式I

eof


# 命令出错会停止执行
set -e
# 打印每条执行的命令
# set -x

string="Bigdata process framework is Hadoop,Hadoop is an open source project" 


function print_tips()
{
    echo "*********************************"
    echo "(1)、打印string长度"
    echo "(2)、删除字符串中所有的Hadoop"
    echo "(3)、替换第一个Hadoop为Mapreduce"
    echo "(4)、替换全部Hadoop为Mapreduce"
    echo "*********************************"
}
function print_string_length()
{
    echo ${#string}
}

function delete_all_hadoop()
{
    echo ${string//Hadoop/}
}

function replace_the_first_hadoop_with_Mapreduce()
{
    echo ${string/Hadoop/Mapreduce}
}
function replace_all_hadoop_with_Mapreduce()
{
    echo ${string//Hadoop/Mapreduce}
}

while true
do
    echo "【string=$string】"
    print_tips
    read -p "Pls input your choice (1|2|3|4|q|Q):" choice

    case $choice in 
        1)
            print_string_length
            ;;
        2)
            delete_all_hadoop
            ;;
        3)
            replace_the_first_hadoop_with_Mapreduce
            ;;
        4)
            replace_all_hadoop_with_Mapreduce
            ;;
        q|Q)
            exit 
            ;;
        *)
            echo "don't have $choice options"
            ;;
    esac
done


set +x
